﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplLogic.ApplClasses;
using ConnectionObjects;
using TCPClient.RequestsToServerHandlers;

namespace TCPClient.UI.LogicForUserControls
{
    public class ScMainUserControlLogicBase
    {
        protected RequestsToServerHandler m_HandlerBase = null;
        protected DateTime m_StartTimeOfTheWeek;
        protected DateTime m_EndTimeOfTheWeek;
        protected DateTime m_StartTimeOfCurrentWeek;
        protected DateTime m_EndTimeOfCurrentWeek;

        public ScMainUserControlLogicBase(RequestsToServerHandler handler)
        {
            m_HandlerBase = handler;
            setNumberOfShiftsPerDay();
            setLenghtOfAShift();
            setStartingHourOfWork();
            CaclulateStartAndEndTimeOfTheWeek(DateTime.Now);
            caclulateStartAndEndTimeOfTheWeek(DateTime.Today, out m_StartTimeOfCurrentWeek, out m_EndTimeOfCurrentWeek);
        }

        public ApplWorkerStatInWeek GetWorkerStat(DateTime startTime, DateTime endTime, string workerId)
        {
            return m_HandlerBase.GetWorkerStat(startTime, endTime, workerId).RetVal as ApplWorkerStatInWeek;
        }

        public ApplWorker GetWorkerById(string workerId)
        {
            return m_HandlerBase.GetWorkerById(workerId).RetVal as ApplWorker;
        }

        public ApplWorkerStatInWeek GetAllWorkerStatOfWorker(string workerId)
        {
            return m_HandlerBase.GetAllWorkerStatOfWorker(workerId).RetVal as ApplWorkerStatInWeek;
        }

        protected void caclulateStartAndEndTimeOfTheWeek(DateTime dateToCalcFor, out DateTime startTime, out DateTime endTime)
        {
            DayOfWeek day = dateToCalcFor.DayOfWeek;
            int days = day - DayOfWeek.Sunday;
            DateTime startBefore = dateToCalcFor.AddDays(-days);
            DateTime endBefore = startBefore.AddDays(6);
            DateTime start = startBefore.AddHours(-startBefore.Hour).AddMinutes(-startBefore.Minute).
                AddSeconds(-startBefore.Second).AddHours(DefaultConfigurationHandler.Instance.StartingTimeOfWork);
            DateTime end = endBefore.AddHours(-endBefore.Hour).AddMinutes(-endBefore.Minute).
                AddSeconds(-endBefore.Second).AddHours(DefaultConfigurationHandler.Instance.StartingTimeOfWork).
                AddHours(DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay * DefaultConfigurationHandler.Instance.LenghtOfShift)
                .AddSeconds(-1);
            start = start.AddMilliseconds(-start.Millisecond);
            end = end.AddMilliseconds(-end.Millisecond);
            startTime = start;
            endTime = end;
        }

        public void CaclulateStartAndEndTimeOfTheWeek(DateTime dateToCalcFor)
        {
            caclulateStartAndEndTimeOfTheWeek(dateToCalcFor, out m_StartTimeOfTheWeek, out m_EndTimeOfTheWeek);
        }

        public DateTime StartTimeOfCurrentWeek
        {
            get { return m_StartTimeOfCurrentWeek; }
            set { m_StartTimeOfCurrentWeek = value; }
        }

        public DateTime EndTimeOfCurrentWeek
        {
            get { return m_EndTimeOfCurrentWeek; }
            set { m_EndTimeOfCurrentWeek = value; }
        }

        public DateTime StartTimeOfTheWeek
        {
            get { return m_StartTimeOfTheWeek; }
            set { m_EndTimeOfTheWeek = value; }
        }

        public DateTime EndTimeOfTheWeek
        {
            get { return m_EndTimeOfTheWeek; }
            set { m_EndTimeOfTheWeek = value; }
        }

        public void setNumberOfShiftsPerDay()
        {
            OutConnectionObject result = m_HandlerBase.GetNumberOfShiftsPerDay();
            DefaultConfigurationHandler.Instance.NumberOfShiftsPerDay = int.Parse(result.RetVal as string);
        }

        public void setLenghtOfAShift()
        {
            OutConnectionObject result = m_HandlerBase.GetLenghtOfShift();
            DefaultConfigurationHandler.Instance.LenghtOfShift = int.Parse(result.RetVal as string);
        }

        public void setStartingHourOfWork()
        {
            OutConnectionObject result = m_HandlerBase.GetStartTimeOfWork();
            DefaultConfigurationHandler.Instance.StartingTimeOfWork = double.Parse(result.RetVal as string);
        }
    }

    public class DefaultConfigurationHandler
    {
        private DefaultConfigurationHandler() { }

        private static DefaultConfigurationHandler m_Instance = null;

        public static DefaultConfigurationHandler Instance
        {
            get { return m_Instance ?? (m_Instance = new DefaultConfigurationHandler()); }
        }

        public int NumberOfShiftsPerDay { get; set; }
        public int LenghtOfShift { get; set; }
        public double StartingTimeOfWork { get; set; }
    }

    public class ShiftRequestFromGUI
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public eType Type { get; set; }
    }
}
